/*
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package com.hn.gege.im.modules.im.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.farsunset.cim.sdk.server.model.Message;
import com.hn.gege.core.common.CommonPage;
import com.hn.gege.core.common.CommonResult;
import com.hn.gege.im.config.component.push.DefaultMessagePusher;
import com.hn.gege.im.modules.im.entity.ChatMessage;
import com.hn.gege.im.modules.im.entity.ChatMessageUnread;
import com.hn.gege.im.modules.im.service.ChatWindowService;
import com.hn.gege.im.modules.im.service.IChatMessageService;
import com.hn.gege.im.modules.im.service.IChatMessageUnreadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;


@RestController
@RequestMapping("/api/message")
@Api(produces = "application/json", tags = "消息相关接口" )
public class MessageController  {

	@Resource
	private DefaultMessagePusher defaultMessagePusher;
	@Autowired
	private IChatMessageService chatMessageService;

	@Autowired
	ChatWindowService chatWindowService;
	@Autowired
	private IChatMessageService messageService;
	@Resource
	private IChatMessageUnreadService chatMessageUnreadService;


	@ApiOperation("发送消息")
	@PostMapping(value = "/send")
	public CommonResult<ChatMessage> send(Message message)  {
		message.setId(System.currentTimeMillis());

		//添加未读消息
		ChatMessage chatMessage = convertMessage(message);
		messageService.add(chatMessage);
		//添加消息记录
		ChatMessageUnread chatMessageUnread = convertMessage2(message);
		chatMessageUnreadService.add(chatMessageUnread);

		//添加接收者的与发送者的聊天窗口
		chatWindowService.add(message.getReceiver(), message.getSender());

		//发送信息
		defaultMessagePusher.push(message);

		return CommonResult.success(chatMessage);
	}

	@ApiOperation("获取未读消息数量")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "sender", value = "发送者账号", required = true, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "receiver", value = "接受者账号", required = true, dataType = "String", paramType = "query")
	})
	@GetMapping("/getCountAndLastMessage")
	public CommonResult<Integer> getCountAndLastMessage(@NotBlank String sender, @NotBlank String receiver){
		Integer count = chatMessageUnreadService.count(sender, receiver);
		return CommonResult.success(count);
	}

	@ApiOperation("获取未读消息分页列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "lastMsgId", value = "最后一条消息id", dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "limit", value = "每页数量", required = true, dataType = "Integer", paramType = "query"),
			@ApiImplicitParam(name = "sender", value = "发送者账号", required = true, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "receiver", value = "接受者账号", required = true, dataType = "String", paramType = "query")
	})
	@GetMapping("/unreadListPage")
	public CommonPage<ChatMessageUnread> unreadListPage(String lastMsgId,
														@NotNull Integer limit,
														@NotBlank String sender,
														@NotBlank String receiver){
		Page<ChatMessageUnread> chatMessageUnreadPage = chatMessageUnreadService.listPageBySenderAndReceiver(lastMsgId, limit, sender, receiver);
		//未读消息返回后,将未读消息删除
		chatMessageUnreadService.remove(sender, receiver);
		return  CommonPage.restPage(chatMessageUnreadPage);
	}
	@ApiOperation("获取未读消息列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "sender", value = "发送者账号", required = true, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "receiver", value = "接受者账号", required = true, dataType = "String", paramType = "query")
	})
	@GetMapping("/unreadList")
	public CommonResult<List<ChatMessageUnread>> unreadList(@NotBlank String sender,
															@NotBlank String receiver){
		List<ChatMessageUnread> chatMessageUnreads = chatMessageUnreadService.listBySenderAndReceiver(sender, receiver);
		//未读消息返回后,将未读消息删除
		chatMessageUnreadService.remove(sender, receiver);
		return  CommonResult.success(chatMessageUnreads);
	}

	@ApiOperation("获取消息分页列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "lastMsgId", value = "最后一条消息id", dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "limit", value = "每页数量", required = true, dataType = "Integer", paramType = "query"),
			@ApiImplicitParam(name = "sender", value = "发送人", required = true, dataType = "Integer", paramType = "query"),
			@ApiImplicitParam(name = "receiver", value = "接收人", required = true, dataType = "Integer", paramType = "query"),
	})
	@GetMapping("/listPage")
	public CommonPage<ChatMessage> listPage(String lastMsgId,
											@NotNull Integer limit,
											@NotBlank String sender,
											@NotBlank String receiver){
		Page<ChatMessage> chatMessagePageInfo = messageService.listPage(lastMsgId, limit, sender, receiver);
		return CommonPage.restPage(chatMessagePageInfo);
	}


	@ApiOperation("未读消息移除")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "sender", value = "发送者账号", required = true, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "receiver", value = "接受者账号", required = true, dataType = "String", paramType = "query")
	})
	@DeleteMapping("/unreadRemove")
	public CommonResult unreadRemove(@NotBlank String sender, @NotBlank String receiver){
		chatMessageUnreadService.remove(sender, receiver);
		return CommonResult.success(null);
	}

	/**
	 * 实体转换
	 * @param message
	 * @return
	 */
	private ChatMessage convertMessage(Message message) {
        ChatMessage chatMessage = new ChatMessage();
        chatMessage.setAction("2");
        chatMessage.setContent(message.getContent());
        chatMessage.setTitle(message.getTitle());
        chatMessage.setSender(message.getSender());
        chatMessage.setReceiver(message.getReceiver());
        chatMessage.setFormat(message.getFormat());
        return chatMessage;

	}

	/**
	 * 实体转换
	 * @param message
	 * @return
	 */
	private ChatMessageUnread convertMessage2(Message message) {
        ChatMessageUnread chatMessage = new ChatMessageUnread();
        chatMessage.setAction("2");
        chatMessage.setContent(message.getContent());
        chatMessage.setTitle(message.getTitle());
        chatMessage.setSender(message.getSender());
        chatMessage.setReceiver(message.getReceiver());
        chatMessage.setFormat(message.getFormat());
        return chatMessage;

	}


}
